package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.pojo.dto.CodeRunDTO;
import com.zenithmind.coding.pojo.vo.CodeRunResultVO;
import com.zenithmind.coding.service.CodeExecutionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 代码执行控制器 - 遵循单一职责原则，只处理代码执行相关功能
 *
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "代码执行", description = "代码运行和调试相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/code/execution")
@RequiredArgsConstructor
public class CodeExecutionController {

    private final CodeExecutionService codeExecutionService;

    @Operation(summary = "运行代码", description = "同步运行代码并返回结果")
    @PostMapping("/run")
    public Result<CodeRunResultVO> runCode(@Valid @RequestBody CodeRunDTO codeRunDTO) {
        CodeRunResultVO result = codeExecutionService.runCode(codeRunDTO);
        return Result.success(result);
    }

    @Operation(summary = "异步运行代码", description = "异步运行代码，返回运行ID")
    @PostMapping("/run/async")
    public Result<String> runCodeAsync(@Valid @RequestBody CodeRunDTO codeRunDTO) {
        String runId = codeExecutionService.runCodeAsync(codeRunDTO);
        return Result.success(runId);
    }

    @Operation(summary = "获取运行结果", description = "根据运行ID获取代码运行结果")
    @GetMapping("/run/{runId}")
    public Result<CodeRunResultVO> getRunResult(
            @Parameter(description = "运行ID") @PathVariable String runId) {
        CodeRunResultVO result = codeExecutionService.getRunResult(runId);
        return Result.success(result);
    }

    @Operation(summary = "停止代码运行", description = "停止正在运行的代码")
    @PostMapping("/run/{runId}/stop")
    public Result<Boolean> stopCodeRun(
            @Parameter(description = "运行ID") @PathVariable String runId) {
        Boolean result = codeExecutionService.stopCodeRun(runId);
        return Result.success(result);
    }


}
